package com.tajiang.business.common.DBUtils;

/**
 * Created by Administrator on 2016/9/29.
 */
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.tajiang.business.common.utils.LogUtils;
import com.tajiang.business.model.Order;
import com.tajiang.business.model.OrderPrinted;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by TITAN on 2015/6/18.
 */

public class OrderPrintDBHelper extends SQLiteOpenHelper {

    private final static int    VERSION = 1;               //数据库版本,大于0
    public final static String DB_NAME = "order_print.db";//数据库文件名称

    private final        String TABLE_SEQUENCE      = "sqlite_sequence";               //自增表名
    private final        String TABLE_ORDER_PRINT      = "order_print";               //已打印的订单表名称

    public final static String COLUMN_ID            = "id";                           //ID
    public final static String COLUMN_ORDER_ID      = "order_id";                    //已打印的订单ID
    public final static String COLUMN_PRINT_TYPE    = "print_type";                  //已打印的订单类型
    public final static String COLUMN_DATE          = "date";                        //已打印的日期时间


    private final String CREATE_TBL_ORDER_PRINT  = "create table " + TABLE_ORDER_PRINT + "(id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "order_id VARCHAR, " +
            "print_type SMALLINT, " +
            "date long)";

    private SQLiteDatabase db;

    //SQLiteOpenHelper子类必须要的一个构造函数
    public OrderPrintDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version) {
        //必须通过super 调用父类的构造函数
        super(context, name, factory, version);
    }
    //数据库的构造函数，传递三个参数的
    public OrderPrintDBHelper(Context context, String name, int version){
        this(context, name, null, version);
    }
    //数据库的构造函数，传递一个参数的， 数据库名字和版本号都固定
    public OrderPrintDBHelper(Context context){
        this(context, DB_NAME, null, VERSION);
    }

    // 回调函数，第一次创建时才会调用此函数，创建一个数据库表
    @Override
    public void onCreate(SQLiteDatabase db) {
        LogUtils.e("Start Create Table..");
        db.execSQL(CREATE_TBL_ORDER_PRINT);
        LogUtils.e("Finished Create Table..");

    }
    //回调函数，当你构造DBHelper的传递的Version与之前的Version调用此函数
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDER_PRINT);
        onCreate(db);
    }


    //返回表指定id的记录
    public OrderPrinted getOrderById(int id) {
        SQLiteDatabase db = getReadableDatabase();
        OrderPrinted orderPrinted = null;
//        Cursor c = db.query(TABLE_ORDER_PRINT, null, COLUMN_ID +"=?", new String[]{String.valueOf(id)}, null, null, null);
        Cursor c = db.query(TABLE_ORDER_PRINT, null, null, null, null, null, null);

        if (c.getCount() > 0) {
            c.moveToLast();
            orderPrinted = new OrderPrinted();
            orderPrinted.setId(c.getInt(c.getColumnIndex(COLUMN_ID)));
            orderPrinted.setOrder_id(c.getString(c.getColumnIndex(COLUMN_ORDER_ID)));
            orderPrinted.setPrint_type(c.getInt(c.getColumnIndex(COLUMN_PRINT_TYPE)));
            orderPrinted.setDate(c.getLong(c.getColumnIndex(COLUMN_DATE)));
        }

        c.close(); //关闭游标
        db.close();   //关闭数据库

        return orderPrinted;
    }

    //返回表记录数量
    public int getOrderCountAndOrder(OrderPrinted orderPrinted) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.query(TABLE_ORDER_PRINT, null, null, null, null, null, null);
        int catCount = c.getCount();
        if (c.getCount() > 0) {
            c.moveToLast();
            if (orderPrinted == null) {
                orderPrinted = new OrderPrinted();
            }
            orderPrinted.setId(c.getInt(c.getColumnIndex(COLUMN_ID)));
            orderPrinted.setOrder_id(c.getString(c.getColumnIndex(COLUMN_ORDER_ID)));
            orderPrinted.setPrint_type(c.getInt(c.getColumnIndex(COLUMN_PRINT_TYPE)));
            orderPrinted.setDate(c.getLong(c.getColumnIndex(COLUMN_DATE)));
        }
        c.close();
        db.close();
        return catCount;
    }

    //插入一条已打印订单记录
    public void insert(OrderPrinted orderPrinted){
        ContentValues values = new ContentValues();

        values.put(COLUMN_ORDER_ID, orderPrinted.getOrder_id());
        values.put(COLUMN_PRINT_TYPE, orderPrinted.getPrint_type());
        values.put(COLUMN_DATE, orderPrinted.getDate());

        //获取SQLiteDatabase实例
        SQLiteDatabase db = getWritableDatabase();
        //插入数据库中
        long rel = db.insert(TABLE_ORDER_PRINT, null, values);

        LogUtils.e("插入数据库返回值： " + rel);

        db.close();

    }

    //查询指定订单ID的记录
    public OrderPrinted queryOrderPrintedByOrderID(String orderId){

        OrderPrinted item = null;
        SQLiteDatabase db = getReadableDatabase();
        //获取OrderPrinted
        Cursor c = db.query(TABLE_ORDER_PRINT, null, COLUMN_ORDER_ID +"=?", new String[]{orderId}, null, null, null);

        while(c.moveToNext()){
            item = new OrderPrinted();
            item.setId(c.getInt(c.getColumnIndex(COLUMN_ORDER_ID)));
            item.setOrder_id(c.getString(c.getColumnIndex(COLUMN_ORDER_ID)));
            item.setPrint_type(c.getInt(c.getColumnIndex(COLUMN_PRINT_TYPE)));
            item.setDate(c.getLong(c.getColumnIndex(COLUMN_DATE)));
            break;
        }

        return item;
    }

    //查询所有已打印的订单表数据
    public Cursor queryTableOrderPrinted(){
        db = getReadableDatabase();
        //获取Cursor
        Cursor c = db.query(TABLE_ORDER_PRINT, null, null, null, null, null, null, null);
        return c;
    }

    //根据唯一标识_id  来删除数据
    public void deleteOrder(String id){
        SQLiteDatabase db = getWritableDatabase();
        db.delete(TABLE_ORDER_PRINT, "id=?", new String[]{id});
        db.close();
    }

    //关闭数据库
    public void close(){
        if(db != null){
            db.close();
        }
    }

    //清楚表
    public void dropTableOrder(){
        SQLiteDatabase db = getWritableDatabase();
        db.delete(TABLE_ORDER_PRINT, null, null);
        db.close();
    }


    /**
     * 业务1：根据指定订单编号返回整条记录
     *
     * @param order_id
     * @return
     */
    public boolean isOrderExist(String order_id) {
        boolean isExist = false;
        SQLiteDatabase db = getReadableDatabase();
        //获取Cursor
        Cursor c = db.query(TABLE_ORDER_PRINT, null, COLUMN_ORDER_ID +"=?", new String[]{order_id}, null, null, null);

        if (c.getCount() > 0) {
            isExist = true;
        }

        c.close(); //关闭游标
        db.close();   //关闭数据库

        return isExist;
    }

}